<?php
class Locator {	
	var $data = array();
	
	function &get($key) {
		if (!isset($this->data[$key])) {
			$this->data[$key] =& $this->create($key);
		}
		
		return $this->data[$key];
	}
	
	function set($key, &$value) {
		$this->data[$key] =& $value;
	}
		
	function create($key) {
		$method = 'create' . $key;
		
		if (method_exists($this, $method)) {
			return $this->$method();
		} else {
			exit('Error: Could not create ' . $key . '!');
		}
	}

	function createCache() {
    	if (!is_writable(DIR_CACHE)) {
      		exit('Error: Could not write to cache directory!');
    	}
			
		require_once(DIR_LIBRARY . 'cache/cache.php');
		
		return new Cache();
	}

	function createConfig() {
		require_once(DIR_LIBRARY . 'application/config.php');
	
		return new Config();
	}

	function createController() {
		require_once(DIR_LIBRARY . 'application/controller.php');
		
		return new Controller($this);
	}

	function createDatabase() {
		require_once(DIR_LIBRARY . 'database/database.php');
		
		$database =& new Database($this);
		
		$database->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
		
		return $database;
	}
	
	function createDownload() {
		require_once(DIR_LIBRARY . 'filesystem/download.php');
		
		return new Download();
	}
	
	function createImage() {
    	if (!is_writable(DIR_IMAGE)) {
    		exit('Error: Could not write to image directory!');
    	}
		
		require_once(DIR_LIBRARY . 'image/image.php');
		
		return new Image($this);
	}
	
	function createLanguage() {
		require_once(DIR_LIBRARY . 'language/language.php');
		
		return new Language($this);
	}

	function createMail() {
		require_once(DIR_LIBRARY . 'mail/mail.php');
		
		return new Mail();
	}
	
	function createModel($key) {
		require_once(DIR_LIBRARY . 'application/model.php');
		
		return new Model($this);
	}
			
	function createModule() {
		require_once(DIR_LIBRARY . 'cart/module.php');
		
		return new Module($this);
	}

	function createRequest() {
		require_once(DIR_LIBRARY . 'environment/request.php');
		
		return new Request($this);
	}

	function createResponse() {
		require_once(DIR_LIBRARY . 'environment/response.php');
		
		return new Response($this);
	}
	
	function createRouter() {
		require_once(DIR_LIBRARY . 'application/router.php');
				
		return new Router($this);
	}
		
	function createSession() {
		require_once(DIR_LIBRARY . 'session/session.php');
		
		return new Session($this);
	}
			
	function createUpload() {
		require_once(DIR_LIBRARY . 'filesystem/upload.php');
		
		return new Upload();
	}
	
	function createUrl() {
		require_once(DIR_LIBRARY . 'environment/url.php');
		
		return new Url($this);
	}			
	
	function createUser() {
		require_once(DIR_LIBRARY . 'user/user.php');
		
		return new User($this);
	}
		
	function createTemplate() {
		require_once(DIR_LIBRARY . 'template/template.php');
		
		$config =& $this->get('config');
		
		return new Template($config->get('config_template'));
	}
}
?>